namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorAverageTrueRange : BaseIndicator
    {
        private LineShape tlsAtr;
        private LineShape tlsMatr;
        private string trField = ("ATR.TR." + Common.GetGuid());

        public IndicatorAverageTrueRange()
        {
            base.IndType = IndicatorType.ATR;
            base.IndName = "ATR";
            base.IndDesc = "真实波动幅度均值";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 14.0));
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "ATR", "MATR" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsAtr) && (obj != this.tlsMatr))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curHigh = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double curLow = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double curValue = Common.CalculateTrueRange(r, curHigh, curLow, base.SourceDic["High"], base.SourceDic["Low"], base.Crab.DataSource);
            double num4 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.tlsAtr.Name, this.trField, curValue, base.Crab.DataSource);
            double num5 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.tlsMatr.Name, this.tlsAtr.Name, num4, base.Crab.DataSource);
            return new double[] { curValue, num4, num5 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsAtr, this.tlsMatr };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsAtr);
            base.Crab.DeleteShape(this.TlsMatr);
            base.Crab.DataSource.Columns.Remove(this.TrField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TrField, numArray[0], r);
                        base.Crab.SetValue(this.TlsAtr.Name, numArray[1], r);
                        base.Crab.SetValue(this.TlsMatr.Name, numArray[2], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.IndPeriod[0].ParamValue = periods[0];
            base.SourceDic["High"] = source[0];
            base.SourceDic["Low"] = source[1];
            this.TlsAtr = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMatr = base.Crab.AddLine(fields[1], panelID);
            base.Crab.DataSource.Columns.Add(this.TrField);
        }

        public LineShape TlsAtr
        {
            get
            {
                return this.tlsAtr;
            }
            set
            {
                this.tlsAtr = value;
            }
        }

        public LineShape TlsMatr
        {
            get
            {
                return this.tlsMatr;
            }
            set
            {
                this.tlsMatr = value;
            }
        }

        public string TrField
        {
            get
            {
                return this.trField;
            }
            set
            {
                this.trField = value;
            }
        }
    }
}

